<?php
namespace Api\Controller;
use Think\Controller;
class HezuoController extends Controller {
    //头条小程序
    public function lnycSubOrderid(){//提交资料
        $username=$_REQUEST['username'];
        $birthday=$_REQUEST['birthday'];
        $sex=$_REQUEST['gender'];
        $uid=$_REQUEST['uid'];
        judgeData(array($username,$birthday,$uid),$_REQUEST);
        cookie('uid',$uid);
        $ymdArr=explode('-',$birthday);
        $datetype=$ymdArr[0];
        $y=$ymdArr[1];
        $m=$ymdArr[2];
        $d=$ymdArr[3];
        $hour=$ymdArr[4];
        $channel='xcxQtt';
        $price=0.01;
        $orderid='YC41X'.date('Ymd').mt_rand(10000,99999).date('His');
        $ymd=$y.'-'.$m.'-'.$d;

        $zyinli=yinli($y,$m,$d);
        cookie('zyangli',$y.'年'.$m.'月'.$d.'日'.' '.cookie('zhour').'时',604800);//阳历
        cookie('zyinli',$zyinli[0].'年'.$zyinli[1].$zyinli[2],604800);//阴历

        $yangli=cookie('zyangli');

        zwmp($ymd,$hour,'男','sizhu');
        $shengxiao=I('globals.shengxiao');//生肖
        $sizhu=I('globals.sizhu');
        $bztaisui=$this->bztaisui2020($sizhu);//犯太岁

        $smH5testinter=array(
            'ordernum'=>$orderid, 'price'=>$price, 'username'=>$username, 'typeid'=>41, 'sex'=>$sex, 'createtime'=>date('Y-m-d H:i:m'),
            'status'=>-1, 'datetype'=>$datetype, 'year'=>$y, 'month'=>$m, 'day'=>$d, 'hour'=>$hour,
            'appuserid'=>$uid, 'ip'=>$_SERVER['REMOTE_ADDR'], 'channel'=>$channel, 'auto_send_sms'=>0, 'place'=>0,
        );
        S('sm_h5test|'.$orderid,$smH5testinter,2592000);//2592000为30天
        cacheZhiFuOrderid($orderid,2);//支付页缓存订单操作

        $Arr=array(
            'code'=>1,
            'orderid'=>$orderid,
            'yangli'=>cookie('zyangli'),
            'yinli'=>cookie('zyinli'),
            'bztaisui'=>"你的出生日期是 阳历 {$yangli},您的八字为{$sizhu[0]} {$sizhu[1]} {$sizhu[2]} {$sizhu[3]}。{$bztaisui}",
            'price'=>$price,
            'username'=>$username,
            'sex'=>$sex,
        );
        echo json_encode($Arr,JSON_UNESCAPED_UNICODE);
    }
    public function lnycSelectOrderid(){//查询是否支付
        $orderid=$_REQUEST['orderid'];
        judgeData(array($orderid),$_REQUEST);
        $arrdata=S('sm_h5test|'.$orderid);
        if($arrdata['status']==1){
            $status=1;
        }else{
            $status=0;
        }
        $Arr=array(
            'code'=>1,
            'status'=>$status,
        );
        echo json_encode($Arr);
    }
    public function lnycOrderList(){//订单列表
        $uid=$_REQUEST['uid'];
        judgeData(array($uid),$_REQUEST);
        $uidArr=S('sm_h5test|'.$uid);
        krsort($uidArr);
        $i=0;
        $arr=array();
        foreach($uidArr as $key=>$value){
            $orderArr=S($value);
            if($i>=10){break;}
            if($orderArr['status']==1&&$orderArr['price']>0&&$orderArr['typeid']==41){$i++;
                $arr[$key]=$orderArr;
            }
        }
        $arr=array_values($arr);
        $arrCount=count($arr);
        $i=0;
        foreach($uidArr as $key=>$value){
            $orderArr=S($value);
            if($i>=10){break;}
            if($orderArr['status']!=1&&$orderArr['price']>0&&$orderArr['typeid']==41){$i++;
                $arr[$arrCount+$i-1]=$orderArr;
            }
        }
        //dump($arr);
        echo json_encode($arr);
    }
    public function lnycOrderJieguo(){//结果数据
        $orderid=$_REQUEST['orderid'];
        judgeData(array($orderid),$_REQUEST);
        $arrdata[0]=jieguoyeArrData($orderid);
        $ymd=$arrdata[0]['year'].'-'.str_pad($arrdata[0]['month'],2,"0",STR_PAD_LEFT).'-'.str_pad($arrdata[0]['day'],2,'0',STR_PAD_LEFT);
        $zymd=$ymd;
        $hour=$arrdata[0]['hour'];
        $username=$arrdata[0]['username'];
        $sex=$arrdata[0]['sex'];
        $datetype=$arrdata[0]['datetype'];
        $y=$arrdata[0]['year'];//年
        $m=$arrdata[0]['month']; //月
        $d=$arrdata[0]['day'];//日
        $zyinli=yinli($y,$m,$d);
        cookie('zyangli',$y.'年'.$m.'月'.$d.'日'.' '.cookie('zhour').'时');//阳历
        cookie('zyinli',$zyinli[0].'年'.$zyinli[1].$zyinli[2]);//阴历

        if($sex==1){$zsex='男';
        }else{$zsex='女';}
        if($ymd!=''){
            zwmp($ymd,$hour,$zsex,'yc');
            $shengxiao=I('globals.shengxiao');//生肖
            $sizhu=I('globals.sizhu');//Z四柱
            $ssarray=array();
            array_push($ssarray,implode(I('globals.nianzhuss')));
            array_push($ssarray,implode(I('globals.yuezhuss')));
            array_push($ssarray,implode(I('globals.shizhuss')));
            $nianzhuss=$ssarray;//十神
            $canggan=I('globals.canggan');//藏干
            $zhishen=I('globals.zhishen');//支神
            $sizhuny=I('globals.sizhuny');//四柱纳音
            $bzdishi=I('globals.bzdishi');//八字地势
            $wxsqs=I('globals.wxsqs');//旺相休囚死
            $xiys=I('globals.xiys');//喜用神
            $taiyuan=I('globals.taiyuan');//胎元
            $rikong=I('globals.rikong');//日空
            $dayun=I('globals.dayun');//大运
            $dayunx=I('globals.dayunx');//大运x
            $dayuny=I('globals.dayuny');//大运y
            $dayungz=I('globals.dayungz');//大运干支
            $dayunss=I('globals.dayunss');//大运十神

            $jdxs2019=jdxs2019($zymd,$hour);//精打细算等等

            $lnyc2020Data=new \Library\Lnyc2020Data();
            $data2020=$lnyc2020Data->index($zymd,$hour);

            $dayun=array(
                'name'=>"出生后{$dayunx}年{$dayuny}月起大运",
                'gz'=>$dayungz,
                'ssArr'=>$dayunss,
                'age'=>array($dayun,$dayun+10,$dayun+20,$dayun+30,$dayun+40,$dayun+50,$dayun+60,$dayun+70,$dayun+80),
                'active'=>0,
            );

            $Arr=array(
                'orderid'=>$orderid,
                'username'=>$arrdata[0]['username'],
                'sex'=>$zsex,
                'yangli'=>cookie('zyangli'),
                'sizhu'=>$sizhu,
                'nianzhuss'=>$nianzhuss,
                'canggan'=>$canggan,
                'zhishen'=>$zhishen,
                'sizhuny'=>$sizhuny,
                'bzdishi'=>$bzdishi,
                'wxsqs'=>$wxsqs,
                'xiys'=>$xiys,
                'taiyuan'=>$taiyuan,
                'rikong'=>$rikong,
                'dayun'=>$dayun,
                'jdxs'=>$jdxs2019,
                'data'=>$data2020,
            );
        }
        echo json_encode($Arr,JSON_UNESCAPED_UNICODE);
    }
    public function code2session(){//小程序登录接口
//        $appid='ttdf89a93440a3ed9d';
//        $appsecret='cea41226b5af430ea8cdb6ce1e44293d0957a874';
        $appid=$_REQUEST['appid'];
        $secret=$_REQUEST['secret'];
        $code=$_REQUEST['code'];
        judgeData(array($appid,$secret,$code),$_REQUEST);
        $url="https://developer.toutiao.com/api/apps/jscode2session?appid={$appid}&secret={$secret}&code={$code}";
        $data=curlGet($url);
        echo $data;
    }
    public function lnycOrderPay(){//支付
        //https://microapp.bytedance.com/dev/cn/mini-app/develop/open-capacity/payment/tt.pay    头条支付文档
        $orderid=$_REQUEST['orderid'];
        $uid=$_REQUEST['uid'];
        $price=$_REQUEST['price'];
        judgeData(array($orderid,$uid,$price),$_REQUEST);
        $merchant_id='1900014549';//头条支付分配给商户的商户号
        $app_id='800145498204';//头条支付分配给商户 app_id，用于获取加签秘钥信息。
        $app_secret='ted2te9dpxn80aerst1wt5skz0e4s4dbfq30cb1h';
        if($price<0.01){
            $Arr=array('code'=>0,'content'=>'价格不能小于0.01');
            echo json_encode($Arr,JSON_UNESCAPED_UNICODE);exit;
        }

        $wx_url=H5WeixinPay($orderid,$price,'流年运程2020-头条');//H5微信支付接口

        $price*=100;
        $strArr=array(
            'app_id'=>$app_id,
            'merchant_id'=>$merchant_id,
            'timestamp'=>time(),
            'sign_type'=>'MD5',
            'out_order_no'=>$orderid,
            'total_amount'=>$price,
            'product_code'=>'pay',
            'payment_type'=>'direct',
            'trade_type'=>'H5',
            'version'=>'2.0',
            'currency'=>'CNY',
            'subject'=>'测试订单',
            'body'=>'测试订单',
            'uid'=>$uid,
            'trade_time'=>time(),
            'valid_time'=>600,
            'notify_url'=>"hy.yixueqm.com/zhiming/index.php/home-index-notify_urlwx?orderid={$orderid}",
            'wx_url'=>$wx_url,
            'wx_type'=>'MWEB',
        );
        $strArr=array_filter($strArr);
        ksort($strArr);
        $strData='';$i=0;
        foreach($strArr as $key=>$value){
            if($i>=1){
                $strData.='&'.$key.'='.$value;
            }else{
                $strData.=$key.'='.$value;
            }
            $i++;
        }
        $sign=md5($strData.$app_secret);

        $strArr['sign']=$sign;
        $strArr['risk_info']="{\"orderid\":\"{$orderid}\",\"content\":\"知命测算\"}";

        echo json_encode($strArr,JSON_UNESCAPED_UNICODE);
    }

    public function bztaisui2020($sizhu){//八字太岁2020
        if(strpos('1'.$sizhu[0],'酉')){
            $data="您属鸡，2020年是“破太岁”的关键年份！";
        }else if(strpos('1'.$sizhu[0],'午')){
            $data="您属马，2020年是“冲太岁”的重要年份！";
        }else if(strpos('1'.$sizhu[0],'未')){
            $data="您属羊，2020年是“害太岁”的年份!";
        }else if(strpos('1'.$sizhu[0],'子')){
            $data="您属鼠，2020年是“坐太岁”的特殊年份。命理称“太岁当年坐，冒犯必有祸”。";
        }else{
            for($i=1;$i<=3;$i++){
                if(strpos('1'.$sizhu[$i],'酉')){
                    $data="你的八字中{$sizhu[$i]}有酉，2020年“破太岁”。";
                    break;}
                if(strpos('1'.$sizhu[$i],'午')){
                    $data="你的八字中{$sizhu[$i]}有午，2020年“冲太岁”。";
                    break;}
                if(strpos('1'.$sizhu[$i],'未')){
                    $data="你的八字中{$sizhu[$i]}有未，2020年“害太岁”。";
                    break;}
                if(strpos('1'.$sizhu[$i],'子')){
                    $data="你的八字中{$sizhu[$i]}有子，2020年“坐太岁”。";
                    break;}
            }
        }
        if(empty($data)){
            $data="2020年庚子年，你的八字中与太岁有“相冲+相克”关系。";
        }
        return $data;
    }
}